package com.xly.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.xly.entity.UserInfo;
import com.xly.service.UserInfoService;
import com.xly.util.JsonUtil;


@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserInfoService userInfoService;
	
	//查询用户列表
	@ResponseBody
	@RequestMapping("/queryList")
	public List<UserInfo> queryList(){
		List<UserInfo> list = userInfoService.queryList();
		return list;
	}
	
	//用户登录
	@ResponseBody
	@RequestMapping(value="/shiroa/login",produces="text/html;charset=UTF-8")
	public JsonUtil login(String phone,String code){
		UserInfo user = userInfoService.selectUser(phone, code);
		if(user != null) {
			return JsonUtil.success(user);
		}
		else {
			return JsonUtil.fail("login fail");
		}
	}
	
	//根据用户名字查询用户信息
	@ResponseBody
	@RequestMapping(value="/getUser",produces="text/html;charset=UTF-8")
	public JsonUtil getUser(String username) {
		UserInfo user = userInfoService.selectByUsername(username);
		if(user != null) {
			return JsonUtil.success(user);
		}
		else {
			return JsonUtil.fail("用户不存在");
		} 
	}
	
	//用户登录-集成shiro
	@ResponseBody
	@RequestMapping("/shiro/login")
	public JsonUtil shiroLogin(String phone,String code,HttpSession session){
		//组装token
		UsernamePasswordToken token = new UsernamePasswordToken(phone, code);
		//会在浏览器生成一个cookie名rememberMe，它是用户名序列后，Base64后的加密字符串
	    token.setRememberMe(true); 
		//获取当前的Subject  
        Subject subject = SecurityUtils.getSubject();
        try {
        	//进入doGetAuthenticationInfo
			subject.login(token);
			//获取用户
			UserInfo user = (UserInfo) session.getAttribute("user");
			return JsonUtil.success(user);
		}
        //如果捕获到异常，就表示验证未通过 
        catch (Exception e) {
        	//e.printStackTrace();
        	return JsonUtil.fail("用户不存在");
		}
	}
	
	//查询账号唯一
/*	@ResponseBody
	@RequestMapping("/checkOne")
	public Map<String, Boolean> checkOne(String email) {
		User user = userService.queryOne(email);
		//初始化固定返回值
		Map<String, Boolean> map = new HashMap<String,Boolean>();
		if(user != null) {
			map.put("valid", false);
		}
		else {
			map.put("valid", true);
		}
		return map;
	}*/
	
	//退出登录
	@ResponseBody
	@RequestMapping("/shiro/logout")
	public String logout() {
		//获取当前的Subject  
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()) {
        	//退出登录，并销毁session
        	subject.logout();
        }
		//返回登录页面
        return "true";
	}
	
	//用户注册
/*	@ResponseBody
	@RequestMapping("/register")
	public boolean register(User user) {
		int i = userService.insertUser(user.getEmail(),user.getPassword(),user.getNickName());
		if(i > 0) {
			return true;
		}
		return false;
	}*/
	
	//用户删除
/*	@ResponseBody
	@RequestMapping("/remove")
	public boolean remove(int id) {
		int i = userService.deleteUser(id);
		if(i>0) {
			return true;
		}
		return false;
	}*/
	
	//用户添加
/*	@ResponseBody
	@RequestMapping("/add")
	public boolean add(User user) {
		int i = userService.insertUser(user.getEmail(), user.getPassword(), user.getNickName());
		if(i>0) {
			return true;
		}
		return false;
	}*/
	
	//用户修改
/*	@ResponseBody
	@RequestMapping("/edit")
	public boolean edit(User user) {
		int i = userService.updateUser(user.getId(), user.getNickName(), user.getCreateDate());
		if(i>0) {
			return true;
		}
		return false;
	}*/
	
	//条件查询
/*	@ResponseBody
	@RequestMapping("/queryCondition")
	public List<User> queryCondition(User user) {
		List<User> list = userService.selectCondition(user);
		return list;
	}*/
	
}
